<template>
  <div class="fenxiao-goods">
    <fxsHeader :type="0" :Data="fxsHeaderData" v-if="!wild_fire_education"></fxsHeader>
    <div class="item-card">
      <div class="item-search">
        <search-box @search="search" :placeholder="Placeholder" :showBtn="showBtn" v-model="keyWord"></search-box>
      </div>
      <form action v-on:submit.prevent>
        <div class="select-item">
          <div class="all-class" @click="clickAllClass">
            <span class="text">{{allClassVal}}</span>
            <i class="icon-right"></i>
          </div>
          <div class="price">
            <input type="text" @keyup.enter="search" v-model="startPrice" placeholder="开始价格" />
          </div>
          <div class="price">
            <input type="text" @keyup.enter="search" v-model="endPrice" placeholder="结束价格" />
          </div>
          <div class="van-search-btn" @click="search">
            <van-button color="#F30C23" text="搜索"></van-button>
          </div>
        </div>
      </form>
    </div>
    <div class="goods-list" v-show="list.length">
      <div class="goods-list-title" v-if="is_agent_item == 1">
        <p>下列商品是否上架</p>
        <van-button round plain hairline class="btn" :class="[sale == 1 ? 'cur':'']" @click="selectSaleItem(1)" text="批量上架"></van-button>
        <van-button round plain hairline class="btn" :class="[sale == 2 ? 'cur':'']" @click="selectSaleItem(0)" text="批量下架"></van-button>
        <!-- <div class="btn" :class="[sale == 1 ? 'cur':'']" @click="selectSaleItem(1)">批量上架</div>
        <div class="btn" :class="[sale == 2 ? 'cur':'']" @click="selectSaleItem(0)">批量下架</div> -->
      </div>
      <div class="sort-search">
        <sort-search :typeList="typeList" @item="selectItem"></sort-search>
      </div>
      <div class="goods-list-items">
        <van-list
          v-model="loading"
          :finished="finished"
          :error.sync="error"
          :immediate-check="immediateCheck"
          error-text="请求失败，点击重新加载"
          finished-text=""
          @load="onLoad"
        >
          <div v-for="(item, index) in list" :key="index" class="list">
            <div @click="JumpUrl(item.link_item)" class="img">
              <img :src="`${item.pic_url}480x480`" v-if="item.is_compress" alt />
              <img :src="`${item.pic_url}`" v-else alt />
              <span v-if="item.discount && item.discount != 10">{{item.discount}}折</span>
            </div>
            <div class="g-flex">
              <p @click="JumpUrl(item.link_item)" class="title">
                <span class="fontZiy" v-if="item.is_self_support == 1">自营</span>{{item.title}}
              </p>
              <p class="g-price">
                <span v-html="scaleGoodsPriceFn(item.price)">
                  <!-- <em class="sym">&yen;</em>
                  <em class="int">{{getInt(item.price)}}</em>
                  <em class="dot fontGilroy">{{getDot(item.price)}}</em> -->
                </span>
                <del v-if="is_display_original_price == 1" class="fontGilroy">
                  <em>{{mSymbol.name}}</em>
                  {{item.original_price}}
                </del>
              </p>
              <span class="commission" v-if="item.is_fx_commission == 1">
                预计{{commission_name}}：<em>{{mSymbol.name}}</em>
                <em class="int fontGilroy">{{item.directly_money}}</em>
              </span>
              <p class="g-fx-btn clearfix">
                <span v-if="is_agent_item==1">
                  <!-- <a
                    href="javascript:;"
                    class="j-comon-sale j-sale"
                    v-if="item.is_sale == 1"
                    @click="selectOneSaleItem(item.is_sale,item.item_id,index)"
                  >
                    <i></i>已上架
                  </a>
                  <a
                    href="javascript:;"
                    class="not j-comon-sale j-nosale"
                    @click="selectOneSaleItem(item.is_sale,item.item_id,index)"
                    v-else
                  >
                    <i></i>未上架
                  </a> -->
                  <van-button class="btn j-sale" text="已上架" round plain color="#E71F19" v-if="item.is_sale == 1" @click="selectOneSaleItem(item.is_sale,item.item_id,index)"></van-button>
                  <van-button class="btn j-nosale" text="未上架" round plain color="#E71F19" v-else @click="selectOneSaleItem(item.is_sale,item.item_id,index)"></van-button>
                </span>
                <span v-if="has_recommend">
                  <!-- <a
                    href="javascript:;"
                    class="j-comon-propose j-has-propose"
                    v-if="item.is_recommend"
                    @click="selectRecommend(item.is_recommend,item.item_id,index)"
                  >已推荐</a>
                  <a
                    href="javascript:;"
                    class="not j-comon-propose j-propose"
                    @click="selectRecommend(item.is_recommend,item.item_id,index)"
                    v-else
                  >未推荐</a> -->
                  <van-button class="btn j-has-propose" round :plain="false" color="linear-gradient(to right,#FE1B49,#E90104)" text="已推荐" v-if="item.is_recommend" @click="selectRecommend(item.is_recommend,item.item_id,index)"></van-button>
                  <van-button class="btn j-has-propose" round :plain="false" color="linear-gradient(to right,#FE1B49,#E90104)" text="未推荐" v-else @click="selectRecommend(item.is_recommend,item.item_id,index)"></van-button>
                </span>
                <span v-else>
                  <!-- <a @click="JumpUrl(item.link_item)">{{fx_good_title}}</a> -->
                  <van-button class="btn" round :plain="false" color="linear-gradient(to right,#FE1B49,#E90104)" @click="JumpUrl(item.link_item)">{{fx_good_title}}</van-button>
                </span>
              </p>
            </div>
          </div>
        </van-list>
      </div>
    </div>
    <div v-show="!list.length && finished">
      <no-data :noData="noData"></no-data>
    </div>
    <van-popup v-model="showAllClass" round position="bottom">
      <van-picker show-toolbar :columns="columns" @cancel="onCancel" @confirm="onConfirm" />
    </van-popup>
  </div>
</template>

<script>
import fxsHeader from '@/views/user/fxs/components/fxsHeader/index'
import noData from '@/components/noData/index'
import sortSearch from '@/views/item/itemList/listWrapper/sortSearch/index'
import searchBox from '@/views/user/components/search/index'
import { getFenxiaoGoods, setFenxiaoGoodsSaleItem } from '@/api/user/fxs/fxsApi'
import { scaleGoodsPrice } from '@/utils/index.js'
import { mapGetters } from 'vuex'
// import Storage from '@/utils/store.js'
import { openPage } from '@/utils/utils'

import Vue from 'vue'
import { Popup, Picker } from 'vant'
Vue.use(Popup).use(Picker)
export default Vue.extend({
  data() {
    return {
      fxsHeaderData: {
        commission_name: '',
        total_commissions: '',
        total_fee: null,
        ftitle_superior_user_order: null,
        superior_user_payment: null,

        show_fxProduct: null,
        fenxiao_goods_count: '',
        ftitle_item: '',

        next_lever_agent_count: '',
        ftitle_agents: '',

        show_myUser: null,
        next_lever_user_count: '',
        ftitle_users: '',

        show_fxOrder: null,
        fenxiao_order_count: '',
        ftitle_fx_order: ''
      },
      // 排序组件控制
      typeList: [
        {
          label: '默认',
          active: false,
          type: 0,
          prop: 'default',
          sort: 'none'
        },
        { label: '价格', active: false, type: 1, prop: 'price', sort: '' },
        {
          label: '销量',
          active: false,
          type: 2,
          prop: 'default',
          sort: 'none'
        },
        {
          label: '人气',
          active: false,
          type: 3,
          prop: 'default',
          sort: 'none'
        },
        {
          label: '',
          active: false,
          type: 4,
          prop: 'default',
          sort: 'none'
        }
      ],
      // 搜索组件控制
      Placeholder: '请输入商品关键字',
      showBtn: true,
      // 所有分组控制
      showAllClass: false,
      group_list: [],
      columns: [],
      allClassVal: '所有分组',
      allClassId: '-1',
      // 显示批量操作
      is_agent_item: '0',
      // 列表
      sale: 0, // 批量操作按钮选中
      has_recommend: 0, // 显示推荐按钮
      fx_good_title: '',
      is_display_original_price: '0', // 显示原价
      list: [],

      // 列表vant组件
      loading: false,
      finished: false,
      error: false,
      immediateCheck: false,

      commission_name: '',

      p: 1,
      keyWord: '',
      startPrice: '',
      endPrice: '',
      search_order: '',

      noData: {
        text: '暂无分销产品',
        img: 'https://img.wifenxiao.com/h5-wfx/images/user/no_distributor.png',
        marginTop: '10em'
      },
      wild_fire_education: 0 // 野火教育定制
    }
  },
  created() {
    this.init()
  },
  mounted() {
    // TODO 小程序触底加载更多
    window.addEventListener('reachbottom', () => {
      this.onLoad()
    })
  },
  methods: {
    scaleGoodsPriceFn(price) {
      return scaleGoodsPrice(price)
    },
    init() {
      this.$loadingWrap.show()
      setTimeout(() => {
        this.$loadingWrap.close()
      }, 50000)
      this.onLoad()
    },
    clickAllClass() {
      this.showAllClass = true
    },
    onCancel() {
      this.showAllClass = false
    },
    onConfirm(val, idx) {
      // console.log(val)
      // console.log(idx)
      if (idx > 0) {
        this.allClassId = this.group_list[idx - 1].group_id
      } else {
        this.allClassId = '-1'
      }
      this.allClassVal = val
      this.showAllClass = false
    },
    allClassList() {
      const list = ['所有分组']
      this.group_list.map(item => {
        list.push(item.title)
      })
      this.columns = list
    },
    onLoad() {
      if (this.finished) return false
      const data = {
        p: this.p,
        title: this.keyWord,
        group_id: this.allClassId,
        start_price: this.startPrice,
        end_price: this.endPrice,
        order: this.search_order
      }
      getFenxiaoGoods(data).then(res => {
        this.loading = false
        this.$loadingWrap.close()
        if (data.p == 1) {
          if (res.status == 1) {
              wx.setNavigationBarTitle({
                title: res.data.ftitle_item
              })
            // 头部和导航数据
            this.fxsHeaderData.commission_name = this.commission_name =
              res.data.commission_name
            this.fxsHeaderData.total_commissions = res.data.total_commissions
            // this.fxsHeaderData.ftitle_fx_order = res.data.ftitle_fx_order
            // 分销产品
            this.fxsHeaderData.show_fxProduct = res.data.show_fxProduct
            this.fxsHeaderData.fenxiao_goods_count =
              res.data._common.fenxiao_goods_count
            this.fxsHeaderData.ftitle_item = res.data.ftitle_item
            // 分销商
            this.fxsHeaderData.next_lever_agent_count =
              res.data._common.next_lever_agent_count
            this.fxsHeaderData.ftitle_agents = res.data.ftitle_agents
            // 会员
            this.fxsHeaderData.show_myUser = res.data.show_myUser
            this.fxsHeaderData.next_lever_user_count =
              res.data._common.next_lever_user_count
            this.fxsHeaderData.ftitle_users = res.data.ftitle_users
            // 订单
            this.fxsHeaderData.show_fxOrder = res.data.show_fxOrder
            this.fxsHeaderData.fenxiao_order_count =
              res.data._common.fenxiao_order_count
            this.fxsHeaderData.ftitle_fx_order = res.data.ftitle_fx_order
            // 搜索部分
            this.group_list = res.data.group_list
            // 判断上架/下架
            this.is_agent_item = res.data.is_agent_item
            // 其他
            this.has_recommend = res.data.has_recommend
            this.is_display_original_price = res.data.is_display_original_price
            this.fx_good_title = res.data.fx_good_title

            // 野火教育定制 start
            this.wild_fire_education = res.data.wild_fire_education
            // 野火教育定制 ent
            
            // 处理其它
            this.allClassList()
            this.typeList[4].label = this.commission_name
            // 页码为1时   初始列表
            this.list = []
            // 列表
            this.list.push(...res.data.lists)
            if (res.data.lists.length < 10) {
              this.finished = true
            } else {
              this.p++
            }
          } else {
            this.$Error(res.msg)
          }
        } else {
          // 列表
          this.list.push(...res)
          if (res.length < 10) {
            this.finished = true
          } else {
            this.p++
          }
        }
      })
    },
    /* getInt(price) {
      if (price) {
        const idx = price.indexOf('.')
        return idx !== -1 ? price.substr(0, idx) : price
      } else {
        return '0'
      }
    },
    getDot(price) {
      if (price) {
        const idx = price.indexOf('.')
        return idx !== -1 ? price.substr(idx) : '.00'
      } else {
        return '.00'
      }
    }, */
    search() {
      this.p = 1
      this.finished = false
      this.error = false
      this.loading = false
      this.$loadingWrap.show()
      setTimeout(() => {
        this.$loadingWrap.close()
      }, 50000)
      this.onLoad()
    },
    // 批量上架/下架
    selectSaleItem(type) {
      const data = {
        type: type ? 'allSale' : 'allNo',
        title: this.keyWord,
        group_id: this.allClassId,
        start_price: this.startPrice,
        end_price: this.endPrice,
        order: this.search_order
      }
      setFenxiaoGoodsSaleItem(data).then(res => {
        if (res.status == 1) {
          this.sale = type ? 1 : 2
          this.list.map(item => {
            return (item.is_sale = type)
          })
          this.$Error(`操作成功！`)
        } else {
          this.$Error(`操作失败！${res.msg}`)
        }
      })
    },
    // 上架/下架
    selectOneSaleItem(type, id, index) {
      const data = {
        type: type ? 'allNo' : 'allSale',
        id
      }
      setFenxiaoGoodsSaleItem(data).then(res => {
        if (res.status == 1) {
          this.$Error(`操作成功！`)
          this.list[index].is_sale = type ? 0 : 1
        } else {
          this.$Error(`操作失败！${res.msg}`)
        }
      })
    },
    // 推荐
    selectRecommend(type, id, index) {
      const data = {
        type: type ? 'unrecommend' : 'recommend',
        id
      }
      setFenxiaoGoodsSaleItem(data).then(res => {
        if (res.status == 1) {
          this.$Error(`操作成功！`)
          this.list[index].is_recommend = type ? 0 : 1
        } else {
          this.$Error(`操作失败！${res.msg}`)
        }
      })
    },
    // 排序方式
    selectItem({ sort, prop, type }) {
      if (type == 0) {
        // 默认
        this.search_order = null
      } else if (type == 1) {
        if (sort == 'ascending') {
          // 价格升
          this.search_order = 'up'
        } else if (sort == 'descending') {
          // 价格降
          this.search_order = 'down'
        }
      } else if (type == 2) {
        if (sort == 'ascending') {
          // 销量升
          this.search_order = 'sales_volume'
        } else if (sort == 'descending') {
          // 销量降   后台暂时没有接口
          // this.search_order = 'sales_volume'
        } else {
          this.search_order = 'sales_volume'
        }
      } else if (type == 3) {
        if (sort == 'ascending') {
          // 人气升
          this.search_order = 'pv'
        } else if (sort == 'descending') {
          // 人气降 后台暂时没有接口
          // this.search_order = 'pv'
        } else {
          this.search_order = 'pv'
        }
      } else if (type == 4) {
        // 默认
        this.search_order = 'commission'
      }
      this.search()
    },
    // Jump(itemid) {
    //   const path = '/mb/item/item-detail'
    //   this.$JumpPath(this, path, { itemid })
    // }
    JumpUrl(url) {
      // window.location.href = url
      openPage(url)
    }
  },
  components: {
    fxsHeader,
    noData,
    sortSearch,
    searchBox
  },
  computed: {
    // 获取自定义符号
    ...mapGetters(['mSymbol'])
  }
})
</script>

<style lang="scss">
@import '../../../../styles/variables';
@import '../../../../styles/mixin';
.fenxiao-goods {
  .item-card {
    margin: 20px 0 0;
    padding: 20px 0;
    overflow: hidden;
    background-color: $default;
    .item-search{
      padding:0 20px;
    }
    .select-item {
      padding: 20px 20px 10px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      flex-wrap: nowrap;
      div {
        // flex: 1;
        &.all-class {
          width: 170px;
          height: 66px;
          // line-height: 66px;
          color: #666;
          text-align: center;
          font-size: 24px;
          border:2px solid #EDEDED;
          display: flex;
          justify-content: center;
          align-items: center;
          .text {
            display: inline-block;
            max-width: 145px;
            @include showEllipsis;
          }
          .svg-icon {
            width: 20px;
            margin-left:4px;
            transform: rotateZ(90deg);
          }
          .icon-right{
            margin-left:4px;
            padding: 4px;
            transform: rotate(45deg);
          }
        }
        &.text {
          width: 120px;
          font-size: 24px;
          color: #333;
          white-space: nowrap;
        }
        &.price {
          @extend .all-class;
          display: flex;
          justify-content: center;
          align-items: center;
          input {
            width: 130px;
            height: 30px;
            border: none;
            line-height: 1.4;
          }
          &:last-child {
            margin-left: 20px;
            position: relative;
            &::after {
              content: '';
              position: absolute;
              left: -17px;
              top: 50%;
              transform: translateY(-50%);
              width: 10px;
              height: 2px;
              background-color: #666;
            }
          }
        }
        &.van-search-btn{
          .van-button{
            width:124px;
            height:60px;
            font-size:28px;
          }
        }
      }
    }
  }
  .goods-list {
    margin: 20px 0;
    padding:10px 0 20px;
    background-color: $default;
    &-title {
      padding:20px 30px 0;
      display: flex;
      align-items: center;
      justify-content: flex-start;
      color: #333;
      .btn {
        height:50px;
        margin-left:20px;
        padding:0 20px;
        color:#666;
        font-size: 22px;
        border-color:#B3B3B3;
        /* width: 150px;
        height: 50px;
        padding-top: 10px;
        // line-height: 52px;
        text-align: center;
        border-radius: 24px;
        background-color: $default;
        border: 2px solid #e8e8e8;
        color: #666;
        font-size: 24px;
        box-sizing: border-box;
        margin-right: 15px; */
        span {
          display: flex;
          justify-content: center;
          align-items: center;
        }
        &.cur {
          border-color:#E71F19;
          color: #F21228;
        }
      }
    }
    // .sort-search{
    //   padding:10px 0;
    // }
    /* &-tab {
      display: flex;
      justify-content: space-between;
      align-items: center;
      span {
        // flex: 1;
        text-align: center;
      }
    } */
    &-items {
      .list {
        display:flex;
        padding-left:20px;
        background: #fff;
        .img {
          padding: 20px 0;
          display: inline-block;
          width: 190px;
          height: 230px;
          overflow: hidden;
          position: relative;
          border-radius: 12px;
          img {
            display:block;
            width: 100%;
            height: 100%;
            border-radius: 12px;
          }
          span {
            display: inline-block;
            overflow: hidden;
            position: absolute;
            top: 0px;
            left: 0px;
            color: #fff;
            background: #e4393c;
            padding: 2px 6px;
          }
        }
        .g-flex {
          flex:1;
          margin-left:20px;
          padding: 20px 20px 20px 0;
          width: 500px;
          height: 230px;
          display: flex;
          flex-direction: column;
          justify-content: space-between;
          border-bottom:2px solid #EDEDED;
          .title {
            font-size: 26px;
            line-height: 34px;
            color: #333;
            @include showEllipsis;
          }
        }
        .g-price {
          display: flex;
          margin: 0;
          margin-top: 10px;
          span {
            color: $warning;
            font-size: 24px;
            em {
              display:inline;
              font-weight: bold;
            }
            /* .int {
              font-size: 36px;
              margin-right: -5px;
            } */
          }
          del {
            display:inline;
            margin-left: 5px;
            color: #999;
            font-size: 24px;
            em{
              display:inline;
            }
          }
        }
        .commission {
          font-size: 24px;
          color:#999;
          em {
            display:inline;
            color: #FF8C01;
            margin-left: -5px;
            font-size: 26px;
            &.int {
              margin-left: -5px;
            }
          }
        }
        .g-fx-btn {
          margin-top: 10px;
          &>span {
            display: inline-block;
            margin-right:16px;
            // float: left;
            // width: 130px;
            // height: 50px;
            // margin-right: 20px;
            // line-height: 52px;
            // text-align: center;
            // border-radius: 25px;
            // overflow: hidden;
            .btn{
              height:50px;
              padding:0 24px;
              font-size:24px;
              text-align:center;
              color:#F21228;
            }
            /* a {
              display: block;
              width: 100%;
              height: 100%;
              color: $default;
              font-size: 24px;
              font-weight: normal;
              color: $default;
              background-color: $warning;
              &.not {
                background-color: #f5f5f5;
                border-color: red;
                color: #666;
              }
            } */
          }
        }
      }
    }
  }
}
</style>
